// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import locale from 'element-ui/lib/locale/lang/en'
import 'element-ui/lib/theme-chalk/index.css'
import VueResource from 'vue-resource'
import axios from 'axios';

// import AppHeader from './components/Header/Header.vue'


Vue.use(VueResource);
Vue.config.productionTip = false;

//axios跨域
axios.defaults.withCredentials=true;

Vue.http.options.credentials=true;
Vue.http.interceptors.push((request, next) => {
  // console.log(this)//此处this为请求所在页面的Vue实例
  // // modify request
  // request.method = 'POST';//在请求之前可以进行一些预处理和配置

  // continue to next interceptor
  next((response) => {//在响应之后传给then之前对response进行修改和逻辑判断。对于token时候已过期的判断，就添加在此处，页面中任何一次http请求都会先调用此处方法

    // response.body = '...';
    // console.log(response);
    var error={message:''};
    switch (response.status) {
      case 400:
        error.message = '请求错误';
        router.replace({path: '/'})
        break
      case 401:
        error.message = '未授权，请登录';
        router.replace({path: '/'})
        break
      case 403:
        error.message = '拒绝访问'
        router.replace({path: '/'})
        break
      case 404:
        // error.message = (process.env.NODE_ENV === 'production' ? `请求地址出错` : `请求地址出错: ${error.response.config.url}`)
        router.replace({path: '/'})
        break
      case 408:
        error.message = '请求超时'
        router.replace({path: '/'})
        break
      case 500:
        error.message = '请重新登录'
        router.replace({path: '/'})
        break
      case 501:
        error.message = '服务未实现'
        router.replace({path: '/'})
        break
      case 502:
        error.message = '网关错误'
        router.replace({path: '/'})
        break
      case 503:
        error.message = '服务不可用'
        router.replace({path: '/'})
        break
      case 504:
        error.message = '网关超时'
        router.replace({path: '/'})
        break
      case 505:
        error.message = 'HTTP版本不受支持'
        router.replace({path: '/'})
        break
      default:
    }
    if(response.status!=200) {
      this.$notify.error({
        message: error.message,
        // type: 'error',
        duration: 5 * 1000
      })
    }
    return response;

  });
});

Vue.use(ElementUI, { locale })
// Vue.component('my-header', AppHeader);

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})
